package logic

import (
	"context"
	"errors"
	"goods/cache"
	"math/rand"
	"time"

	"goods/goodsrpc/goodsrpc"
	"goods/goodsrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *goodsrpc.SendSmsRequest) (*goodsrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line

	i2, _ := cache.Rdb.Get(cache.Ctx, "sendSms"+in.Mobile).Int()
	if i2 >= 1 {
		return nil, errors.New("一分钟内只能发送一次")
	}

	code := rand.Intn(9000) + 1000
	//sms, _ := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	//if sms.Code == 2 {
	//	return nil, errors.New(sms.Msg)
	//}
	cache.Rdb.Set(cache.Ctx, "sendSms"+in.Mobile, code, time.Minute*5)
	//result, _ := cache.Rdb.Get(cache.Ctx, "sendSms"+in.Mobile).Result()
	//if result == "" {
	//	return nil, errors.New("一分钟内只能发送一次")
	//}
	return &goodsrpc.SendSmsResponse{}, nil
}
